﻿//***********************************************************************
 //* Module:  TbBstOnlineAlarmDAL
// * 作者:  曹荣芝
 //* Modified: 2013-04-02
// * Purpose: Definition of the Class TbBstOnlineAlarmDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.Framework.AuthEntity;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data.OracleClient;
namespace XWG.JWBST.JWBSTDAL
{
	/// <summary>
	/// 
	/// </summary>
	public class TbBstOnlineAlarmDAL
	{
          #region 删除操作
                /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="onlinealarmpkid">要删除的的主键值。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object onlinealarmpkid,object userid,bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                 JWBSTEntity.TbBstOnlineAlarm u = JWBSTDAL.TbBstOnlineAlarmDAL.Load(onlinealarmpkid) as JWBSTEntity.TbBstOnlineAlarm;
             
                if (isLog)//如果写入日志
                {
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "删除";
                    log.LogObject = TbBstOnlineAlarmConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransDelete<TbBstOnlineAlarm, AppLog>(u, log);
                }
                else//不写入日志
                {
                Db.TransDelete(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(onlinealarmpkid);
                resultModel.ISSucceed = true;
                return resultModel;     
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(onlinealarmpkid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;
                
            }
        }
        #endregion
         
        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="onlinealarmpkid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstOnlineAlarm Load(object onlinealarmpkid)
        {
              return Db.GetModel<JWBSTEntity.TbBstOnlineAlarm>(onlinealarmpkid); 
        }
        #endregion
        
        #region 查询操作
       /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstOnlineAlarm> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstOnlineAlarm));
                //crt.Add(NHibernate.Criterion.Expression.Eq(TbBstOnlineAlarmConst.Columns.EnabledPropertyName, Enabled.TRUE));
                IList<TbBstOnlineAlarm> list = crt.List<TbBstOnlineAlarm>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {
                
            }
        }
        #endregion
        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstOnlineAlarm info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {
                    
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "新建";
                    log.LogObject = TbBstOnlineAlarmConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    filedkey= Db.TransInsert<TbBstOnlineAlarm,AppLog>(info,log);
                }
                else
                {
                   filedkey=  Db.TransInsert<TbBstOnlineAlarm>(info);
                }
                 resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }   
        }
		#endregion 
        
        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstOnlineAlarm info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                 if (isLog)
                {
                     
                    
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "修改";
                    log.LogObject = TbBstOnlineAlarmConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransUpdate<TbBstOnlineAlarm, AppLog>(info, log);
                }
                else
                {
                  Db.TransUpdate<TbBstOnlineAlarm>(info);
                }
                resultModel.ErrorCode = 0;
                 resultModel.FiledKey = "";
                 resultModel.ISSucceed = true;
                 return resultModel;
            }
            catch (Exception ex)
            {
              resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
              MSEx.DealErr(ex, "实体类的修改操作");
              return resultModel;
            }   
        }
		#endregion 

        #region 插入并返回ID
        /// <summary>
        /// 插入报警信息并返回ID
        /// </summary>
        /// <param name="online">报警信息</param>
        /// <returns>true成功.false失败</returns>
        public static bool InsertGetID(TbBstOnlineAlarm online)
        {
           
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into TB_BST_ONLINE_ALARM(");
			strSql.Append("ONLINE_ALARM_PKID,USER_ID,PHONE_NO,GPS_X,ALARM_TYPE,ALARM_MESSAGE,ALARM_DATE,PROCESS_RESULT,PROCESS_DATE,GPS_Y)");
			strSql.Append(" values (");
			strSql.Append(":ONLINE_ALARM_PKID,:USER_ID,:PHONE_NO,:GPS_X,:ALARM_TYPE,:ALARM_MESSAGE,:ALARM_DATE,:PROCESS_RESULT,:PROCESS_DATE,:GPS_Y)");
			OracleParameter[] parameters = {
					new OracleParameter(":ONLINE_ALARM_PKID", online.OnlineAlarmPkid),
					new OracleParameter(":USER_ID", online.UserId),
					new OracleParameter(":PHONE_NO", online.PhoneNo),
					new OracleParameter(":GPS_X", online.GpsX),
					new OracleParameter(":ALARM_TYPE", online.AlarmType),
					new OracleParameter(":ALARM_MESSAGE", online.AlarmMessage),
					new OracleParameter(":ALARM_DATE", online.AlarmDate),
					new OracleParameter(":PROCESS_RESULT", online.ProcessResult),
					new OracleParameter(":PROCESS_DATE", online.ProcessDate),
					new OracleParameter(":GPS_Y", online.GpsY)};

            int rows = OrclHelper.ExecuteOracle(strSql.ToString(), parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
        }
        #endregion
    }
}
